using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Pervote.Core.Logic;
using Pervote.Core;

public partial class CreatePoll : MinimalPageBase
{
    new protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Populate the category-list : Ugly
            DataTable cats = new CategoryManager().GetRootCategories();
            ListItem item;
            item = new ListItem("Select Category", "-1");
            categoryList.Items.Add(item);
            foreach (DataRow cat in cats.Rows)
            {
                item = new ListItem((string)cat["Name"], cat["Id"].ToString());
                categoryList.Items.Add(item);
            }
            categoryList.DataBind();
            categoryList.SelectedIndex = 0;

            createPollButton.Attributes.Add("onclick", "return inputsValid(" + categoryList.UniqueID + ");");
        }
    }

    protected void createPollButton_Click(object sender, EventArgs e)
    {
        MembershipUser user = Membership.GetUser();
        if (user == null)
        {
            Response.Redirect("/login.aspx");
            return;
        }

        Poll poll = new Poll();
        poll.Title = HtmlHelper.Sanitize(Request["pollTitle"]);
        if (string.IsNullOrEmpty(poll.Title)) return;
        poll.CategoryId = long.Parse(categoryList.SelectedValue);
        poll.StartedBy = user.UserName;
        poll.Description = (Request["pollDescription"] != null) ? HtmlHelper.Sanitize(Request["pollDescription"]) : "";
       
        if (Request["allowMultipleVotes"] == "on") poll.AllowMultipleVotes = true;
        else poll.AllowMultipleVotes = false;

        if (Request["allowAddItems"] == "on") poll.ItemsLocked = false;
        else poll.ItemsLocked = true;
        
        PollManager pm = new PollManager();
        ErrorInfo error = new ErrorInfo();
        long pollId = pm.CreatePoll(poll, error);

        PollItem item = null;
        PollItemComment comment = null;
        int itemCount = 0;
        while (Request.Form["itemName" + (itemCount + 1)] != null)
        {
            itemCount++;
            item = new PollItem();
            item.Name = HtmlHelper.Sanitize(Request.Form["itemName" + itemCount]);
            if (String.IsNullOrEmpty(item.Name)) continue;
            
            item.PollId = pollId;
            item.AddedBy = user.UserName;

            long itemId = pm.AddItem(item, error);

            if (Request.Form["itemDescription" + itemCount] != null)
            {
                comment = new PollItemComment();
                comment.Text = HtmlHelper.Sanitize(Request["itemDescription" + itemCount]);
                comment.Link = (Request["itemLink" + itemCount] != null) ? HtmlHelper.Sanitize(Request["itemLink" + itemCount]) : "";
                comment.AddedBy = user.UserName;
                pm.AddItemComment(itemId, comment, error);
            }
        }
        Response.Redirect("/PollCreated.aspx?pollId=" + pollId.ToString());
    }
}
